library FragGrenade initializer init requires LegacySystem

	private function Trig_Frag_Grenade_Conditions takes nothing returns boolean
		if ( not ( GetSpellAbilityId() == 'A08S' ) ) then
			return false
		endif
		return true
	endfunction

	private function FriendlyFire takes player p ,location l, integer area returns boolean
		local group g =  GetUnitsInRangeOfLocAll(area, l)
		local unit a 

		loop
			exitwhen IsUnitGroupEmptyBJ(g) == true
			set a = GroupPickRandomUnit(g)
			if( IsUnitAlly(a, p) ) then
				if(IsUnitType(a, UNIT_TYPE_HERO)) then
					call DestroyGroup(g)
					set a = null
					set g = null
					return true
				endif
			endif  
			call GroupRemoveUnitSimple( a, g)
		endloop

		
		call DestroyGroup(g)
		set a = null
		set g = null
		return false

	endfunction

	private function Trig_Frag_Grenade_Actions takes nothing returns nothing
		local location target = GetSpellTargetLoc()
		local unit caster = GetTriggerUnit()
		local real x = GetLocationX(target)
		local real y = GetLocationY(target) 
		local unit missile 
		local player p = GetOwningPlayer(caster)
		local integer i = GetConvertedPlayerId(p)
		local rect r 
		local group g
		
		// control stuff
		local real innerDamage = 100.0 + 200.0 * I2R(GetUnitAbilityLevel(caster, GetSpellAbilityId()) - 1) 
		local real outerDamage = 200.0

		local integer innerAOE = 200 // the area of the blast
		local integer outerAOE = 330 // the area of the shrapnel

		call TriggerSleepAction(0.3)
		set missile = CreateUnit(Player(11),'h00H',GetUnitX(caster),GetUnitY(caster),0.0)
		call IssuePointOrder(missile,"attackground",x,y)

		if(GetRandomInt(1,2) == 1) then
			call FieldChat(caster,2.0, udg_Pcolors[i]  + GetPlayerName(p)+ "|r : Fire in the hole!")
		else 
			call FieldChat(caster,2.0, udg_Pcolors[i]  + GetPlayerName(p)+ "|r : Grenade!")
		endif

		call TriggerSleepAction(2.00)

		if (FriendlyFire(p,target,innerAOE) == false or udg_Experience[i] > 5000) then
		
		  call EarShockArea(x, y, 250)
		  call GernadeStunAOE(x,y)
		  //call DestroyEffect(AddSpecialEffect( "Effect_NewGroundEx.mdl",x,y ))
		  
		  call DestroyEffect(AddSpecialEffect( "Objects\\Spawnmodels\\Undead\\ImpaleTargetDust\\ImpaleTargetDust.mdl",x,y ))
		  call DestroyEffect(AddSpecialEffect( "abilities\\weapons\\DemolisherMissile\\DemolisherMissile.mdl",x,y ))
		  
		  call UnitDamagePoint( caster,0, innerAOE,x,y,innerDamage,true,false,ATTACK_TYPE_PIERCE,DAMAGE_TYPE_NORMAL,WEAPON_TYPE_WHOKNOWS )
		  call UnitDamagePoint( caster,0, outerAOE,x,y,outerDamage,true,false,ATTACK_TYPE_PIERCE,DAMAGE_TYPE_NORMAL,WEAPON_TYPE_WHOKNOWS )
		
		  set r = Rect( x-100.0,y-100.0,x+100.0,y+100.0)
		  set g = CreateGroup()
		  call GroupEnumUnitsInRect(g, r, Condition(function killexplosiveFast))
		  call DestroyGroup(g)
		  call RemoveRect(r)
		  
		  set bj_meleeNearestMineDist = x
		  set bj_lastTransmissionDuration = y
		  set udg_soundpath = "war3mapImported\\Explosion2mono.mp3"
		  call PlaySoundPath3D.execute()
		endif

		call RemoveUnit(missile)
		call RemoveLocation(target)
		set target = null
		set caster = null
		set missile = null
		set p = null
		set g = null
		set r = null

	endfunction
	
	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger(  )
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
		call TriggerAddCondition( t, Condition( function Trig_Frag_Grenade_Conditions ) )
		call TriggerAddAction( t, function Trig_Frag_Grenade_Actions )
	endfunction

endlibrary
